#! /bin/sh

if [ "${#}" != 2 ] ; then
	echo "Exactly two parameters requires: mklicense {dest} {src}."
	exit 1
fi

if [ ! -f "${2}" ] ; then
	echo "Source license file does not exist: ${2}."
	exit 1
fi

prjName="$(awk 'match($0, /\`[a-z-]*\`/ ){ printf( toupper( substr( $0, RSTART + 1, RLENGTH - 2 ) ) ); nextfile }' "${2}")"

cat > "${1}" << EOF
#ifndef ${prjName}_LICENSE_HXX_INCLUDED
#define ${prjName}_LICENSE_HXX_INCLUDED 1

char const LICENSE[] =
EOF

# FreeBSD sed(1) does not support \t hence literal <tab>.
sed -e 's/^/	"/' -e 's/$/\\n"/' -e 's/`[a-z]*`\([^0-9-]*\)[0-9-]\{1,\}/`%s`\1%s/' "${2}" >> "${1}"

cat >> "${1}" << EOF
;

#endif /* #ifndef ${prjName}_LICENSE_HXX_INCLUDED */

EOF
